<template>
  <div>
    <el-form ref="form" :model="searchForm" :inline="true">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="请输入要查询影片"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchFilmInfo">搜索</el-button>
        <el-button type="primary" @click="dialogFormVisible=true">新建影片</el-button>
        <el-popconfirm title="确认要批量删除吗！！！！" @confirm="handleDelete">
          <el-button slot="reference" type="danger" :disabled="deleteAllBtnStu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table  :data="tableData" border stripe style="width: 100%" :cell-style="{padding:'2px'}"
              @selection-change="selectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column prop="cover" label="电影封面" width="200px">
        <template slot-scope="scope">
          <el-image style="height: 100px; " :src="scope.row.cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="影片名称"></el-table-column>
      <el-table-column prop="release_time" label="上映日期"></el-table-column>
      <el-table-column prop="category_id" label="类别"></el-table-column>
      <el-table-column prop="region" label="地区"></el-table-column>
      <el-table-column prop="statu" label="状态">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.statu===1 " type="success">上架</el-tag>
          <el-tag size="mini" v-else-if="scope.row.statu===0 " type="danger">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="duration" label="时长(min)"></el-table-column>
      <el-table-column prop="grade" label="评分" width="200px">
        <template slot-scope="scope">
        <el-rate
            v-model="scope.row.grade"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}">
        </el-rate>
        </template>
      </el-table-column>


      <el-table-column label="操作" width="340">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认要删除吗！！！！" @confirm="handleDelete(scope.row.id)">
            <el-button slot="reference" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="影片信息" :visible.sync="dialogFormVisible" width="600px" :before-close="handleClose">
      <el-form :model="FilmForm" ref="FilmForm" label-width="100px" :rules="rules">
        <el-form-item label="用户头像" label-width="100px">
          <el-upload
              class="avatar-uploader" action="/api/upload" :show-file-list="false"
              :on-success="handleAvatarSuccess" :headers="FilmForm.cover">
            <img v-if="FilmForm.cover" :src="FilmForm.cover" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="影片名称" prop="name">
          <el-input v-model="FilmForm.name" ></el-input>
        </el-form-item>
        <el-form-item label="上映日期" prop="release_time">
          <el-date-picker type="date" v-model="FilmForm.release_time" ></el-date-picker>
        </el-form-item>
        <el-form-item label="类别">
          <el-input v-model="FilmForm.category_id"></el-input>
        </el-form-item>
        <el-form-item label="地区">
          <el-input v-model="FilmForm.region"></el-input>
        </el-form-item>
        <el-form-item label="时长">
          <el-input-number v-model="FilmForm.duration"></el-input-number>
        </el-form-item>
        <el-form-item label="评分">
          <el-input-number v-model="FilmForm.grade"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" label-width="100px">
          <el-radio-group v-model="FilmForm.statu">
            <el-radio :label="0">上架</el-radio>
            <el-radio :label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>


export default {
  data() {
    return {
      searchForm:{},
      films: [],
      dialogFormVisible: false,
      FilmForm:{},
      tableData:[],
      deleteAllBtnStu: true,
      rules: {
        name: [
          { required: true, message: '请输入影片名称', trigger: 'blur' }
        ],
        release_time: [
          { required: true, message: '请输入上映日期', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.loadFilms()
  },
  methods:{
    searchFilmInfo(){

    },
    handleAvatarSuccess(response) {
      this.FilmForm.cover = response.data.data.cover
    },
    handleClose(){
      this.resetForm()
      this.dialogFormVisible = false;
    },
    loadFilms(){
      this.$axios.get('/system/film/list').then(response => {
        this.tableData = response.data.data
      })
    },
    handleEdit(id){
      this.$axios('/film/userInfo/' + id).then(response => {
        this.FilmForm = response.data.data
        this.dialogFormVisible = true
      })
    },
    handleDelete(id){
      let ids = [];
      id ? ids.push(id) : this.selection.forEach(u => {
        ids.push(u.id)
      })
      this.$axios.post('/system/film/delete', ids).then(response => {
        this.$message({
          message: response.data.message,
          type: 'success',
          showClose: true,
          onClose: () => {
            this.loadFilms()
          }
        });
      })
    },
    handleSave(formName){
      this.$refs.FilmForm.validate(valid => {
        if (valid) {
          this.$axios.post('/system/film/' + (this.FilmForm.id ? 'update' : 'save'), this.FilmForm).then(response => {
            this.$message({
              message: response.data.message,
              type: 'success',
              // showClose: true,
              duration:1000,
              onClose: () => {
                this.loadFilms()
                this.dialogFormVisible = false
                this.resetForm(formName)
              }
            })
          })
        }
      })
    },
    resetForm() {
      this.FilmForm = {}
      this.$refs.FilmForm.resetFields();
    },
    selectionChange(value){
      this.selection = value;
      if (this.selection) {
        this.deleteAllBtnStu = (this.selection.length === 0)
      }
    }
  }
}
</script>
<style scoped>
.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}
</style>